<?php
// No directAcces
defined( '_AppBackMiage' ) or die( 'Restricted access' );
if(isAdministrateur ()){
    include_once (DS."backoffice/include/header.php");

        if(isset($_POST['submit']))
        {
            $enseignant = new Enseignant();
            // variable qui va nous servir pour savoir s'il y a a eu une erreur ;)
            $form = true;
            $errMSG = array();
            if(isset($_POST['lastname']) && !empty($_POST['lastname']))
            {
                $tmp = $enseignant->setNomUtilisateur($_POST['lastname']);
                if($tmp['boolOk'] === false)
                {
                    $errMSG['nom'] = $tmp['errmsg'];
                    $form = false;
                }
            }
            if(isset($_POST['firstname']) && !empty($_POST['firstname']))
            {
                $tmp = $enseignant->setPrenomUtilisateur($_POST['firstname']);
                if($tmp['boolOk'] === false)
                {
                    $errMSG['prenom'] = $tmp['errmsg'];
                    $form = false;
                }
            }
            if(isset($_POST['password']) && !empty($_POST['password']) && isset($_POST['password2']) && !empty($_POST['password2']))
            {
                $tmp = $enseignant->setPassword($_POST['password']);
                if($tmp['boolOk'] === false)
                {
                    $errMSG['password'] = $tmp['errmsg'];
                    $form = false;
                }
            }
            if(isset($_POST['email']) && !empty($_POST['email']))
            {
                $tmp = $enseignant->setEmailUtilisateur($_POST['email']);
                if($tmp['boolOk'] === false)
                {
                    $errMSG['mail'] = $tmp['errmsg'];
                    $form = false;
                }
            }

            if(isset($_POST['ufr']) && !empty($_POST['ufr']))
            {
                $tmp = $enseignant->setIdUFR($_POST['ufr']);
                if($tmp['boolOk'] === false)
                {
                    $errMSG['ufr'] = $tmp['errmsg'];
                    $form = false;
                }
            }

                        if(isset($_POST['types']) && !empty($_POST['types']))
            {
                $types = implode(',',$_POST['types']);
                $tmp = $enseignant->setIdTypeUser($types);
                if($tmp['boolOk'] === false)
                {
                    $errMSG['types'] = $tmp['errmsg'];
                    $form = false;
                }
            }

            if($form === true)
            {
                $enseignant->insert();
                redirectTo(URLBACK."_userenseignant_list");
            }
        }

    $lstTypes = TypeUtilisateur::getList();
    $lstUFR = UFR::getList();

    include_once( DS."backoffice/include/toppanel_utilisateurs.php");
    ?>
    <div id="wrapper">
        <div id="content">
            <div id="box">
                <h3 id="adduser">Ajout d'un enseignant</h3>
                <form id="form" method="post">
                <fieldset id="personal">
                    <legend>Informations Personnelles</legend>
                    <label for="lastname">Nom : </label>
                    <input name="lastname" id="lastname" type="text" tabindex="1" />
                    <br />
                    <label for="firstname">Prenom : </label>
                    <input name="firstname" id="firstname" type="text" tabindex="2" />
                    <br />
                    <label for="password">Password : </label>
                    <input name="password" id="password" type="text"tabindex="2" />
                    <br />
                    <label for="password2">re-Password : </label>
                    <input name="password2" id="password2" type="text"tabindex="2" />
                    <br />
                    <label for="email">Email : </label>
                    <input name="email" id="email" type="text"tabindex="2" />
                </fieldset>
                <fieldset>
                    <legend>OPTIONS</legend>
                    <label for="ufr">UFR : </label>
                    <select name="ufr">
                    <?php
                    if(!empty($lstUFR))
                    {
                        foreach($lstUFR as $id=>$p)
                        {
                            ?>
                            <option value="<?php echo $id; ?>"><?php echo $p; ?></option>
                            <?php
                        }
                    }
                    ?>
                    </select>
                    <br/>
                    <label for="types">Type d'utilisateur : </label>
                    <?php
                    if(!empty($lstTypes))
                    {
                        foreach($lstTypes as $type)
                        {
                            ?>
                            <?php echo $type->NomTypeUtilisateur; ?>
                            <input type="checkbox" name="types[]" value="<?php echo $type->IdTypeUtilisateur; ?>">
                            &nbsp;|&nbsp;
                            <?php
                        }
                    }
                    ?>
                </fieldset>
                <div align="center">
                    <input id="button1" type="submit" name="submit" value="Ajouter" />
                    <input id="button2" type="reset" />
                </div>
                </form>
                <br /><span class="error"><?php if(isset($errMSG['types'])){ echo $errMSG['types']; } ?></span>
            </div>
            </div>
    <?php
    include_once (DS."backoffice/include/sidebar.php");
    include_once (DS."backoffice/include/footer.php");
}
else
{
    redirectTo(URLBACK);
}
?>